/* 
** $Id: mycontrol.c 741 2009-03-31 07:16:18Z weiym $
**
** Listing 3.1
**
** mycontrol.c: Sample program for MiniGUI Programming Guide
**      Use my own control to print "Hello, world!".
**
** Copyright (C) 2004 ~ 2007 Feynman Software.
**
** License: GPL
*/

#include <stdio.h>

#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>

#define MY_CTRL_NAME "mycontrol"

HWND timeedit;
#define IDC_EDIT    100
#define IDC_SPINBOX 110

static PLOGFONT timefont;

static WNDPROC old_edit_proc;
HWND mycontrol_01=0;
HWND hcontrlwnd_01=0;
HWND htimeedit_01=0;



static void on_down_up (HWND hwnd, int offset)
{
    char time [10];
    int caretpos;
    int hour, minute, second;

    GetWindowText (hwnd, time, 8);
    caretpos = SendMessage (hwnd, EM_GETCARETPOS, 0, 0);
	//printf("");

    hour = atoi (time);
    minute = atoi (time + 3);
    second = atoi (time + 6);

    if (caretpos > 5) { /* change second */
        second += offset;
        if (second < 0)
            second = 59;
        if (second > 59)
            second = 0;
    }
    else if (caretpos > 2) { /* change minute */
        minute += offset;
        if (minute < 0)
            minute = 59;
        if (minute > 59)
            minute = 0;
    }
    else { /* change hour */
        hour += offset;
        if (hour < 0)
            hour = 23;
        if (hour > 23)
            hour = 0;
    }

    sprintf (time, "%02d:%02d:%02d", hour, minute, second);
    SetWindowText (hwnd, time);
	//SetWindowBkColor(HWND hWnd,int new_bkcolor);
	//SetWindowBkColor(mycontrol_01,PIXEL_lightwhite);
	
	//SetWindowElementAttr(mycontrol_01,WE_BGC_WINDOW,PIXEL_yellow);
	//SetWindowElementAttr(mycontrol_01,WE_BGC_WINDOW,PIXEL_lightwhite);
	
#if 1
	//SetWindowElementAttr(hcontrlwnd_01,WE_FGC_WINDOW,PIXEL_black);
	SetWindowElementAttr(hcontrlwnd_01,WE_FGC_WINDOW,PIXEL_yellow);
	//InvalidateRect (hcontrlwnd_01, NULL, TRUE);
	
	//SetWindowElementAttr(mycontrol_01,WE_FGC_WINDOW,PIXEL_yellow);
	//SetWindowBkColor(mycontrol_01,PIXEL_yellow);
	SetWindowElementAttr(htimeedit_01,WE_FGC_WINDOW,PIXEL_yellow);
	InvalidateRect (mycontrol_01, NULL, TRUE);
#endif
	
	InvalidateRect (timeedit, NULL, TRUE);
    SendMessage (hwnd, EM_SETCARETPOS, 0, caretpos);
}

static int TimeEditBox (HWND hwnd, int message, WPARAM wParam, LPARAM lParam)
{
			printf("%s;%s;%d;wParam=%d\r\n",__FILE__,__func__,__LINE__,wParam);
    if (message == MSG_KEYDOWN) {
		printf("%s;%s;%d;wParam=%d\r\n",__FILE__,__func__,__LINE__,wParam);
        switch (wParam) {
        case 13: //SCANCODE_CURSORBLOCKUP:
            on_down_up (hwnd, 1);
            return 0;
        case 27: //SCANCODE_CURSORBLOCKDOWN:
            on_down_up (hwnd, -1);
            return 0;
        case 40: //SCANCODE_PAGEUP:
            //on_down_up (hwnd, 10);
            
            return 0;
        case 37: //SCANCODE_PAGEDOWN:
            //on_down_up (hwnd, -10);
            {
            int caretpos = SendMessage (hwnd, EM_GETCARETPOS, 0, 0);
            SendMessage (hwnd, EM_SETCARETPOS, 0, caretpos+1);
            }
            return 0;

        case SCANCODE_CURSORBLOCKLEFT:
        case SCANCODE_CURSORBLOCKRIGHT:
                break;
        default:
                return 0;
        }
    }

    if (message == MSG_KEYUP || message == MSG_CHAR)
        return 0;

    return (*old_edit_proc) (hwnd, message, wParam, lParam);
}


static int MyControlProc (HWND hwnd, int message, WPARAM wParam, LPARAM lParam)
{
    HDC hdc;
	//printf("%s;%s;%d;wParam=%d\r\n",__FILE__,__func__,__LINE__,wParam);
    switch (message) {
		case MSG_CREATE:
		{
			#if 1
			HWND hcontrlwnd;
			HDC hdc;
			//HWND timeedit, spin;
			SIZE size;
			printf("MSG_CREATE;%s;%s;%d;wParam=%d\r\n",__FILE__,__func__,__LINE__,wParam);
			#if 1
			timefont = CreateLogFont (NULL, "System", "ISO8859-1", 
							FONT_WEIGHT_BOOK, FONT_SLANT_ROMAN, FONT_FLIP_NIL,
							FONT_OTHER_AUTOSCALE, FONT_UNDERLINE_NONE, FONT_STRUCKOUT_NONE, 
							16, 0);
		#endif
			hcontrlwnd = CreateWindow (CTRL_STATIC, "PROMPT", 
							WS_CHILD | WS_VISIBLE | SS_LEFT, 
							IDC_STATIC, 
							10, 20, 180, 20, hwnd, 0);
			//SetWindowBkColor (hwnd, PIXEL_blue);
			//SetWindowBkColor (hwnd, COLOR_transparent);	
			//SetWindowElementColorEx (hwnd, FGC_CONTROL_NORMAL, PIXEL_lightwhite);
			SetWindowFont (hcontrlwnd, timefont);

			InvalidateRect (hcontrlwnd, NULL, TRUE);
			if(hcontrlwnd_01==0)
				hcontrlwnd_01=hcontrlwnd;

			#endif
		#if 1

		
			hdc = GetClientDC (hwnd);
			SelectFont (hdc, timefont);
			GetTextExtent (hdc, "00:00:00", -1, &size);
			ReleaseDC (hdc);
		
			timeedit = CreateWindow (CTRL_SLEDIT, 
							"00:00:00", 
							WS_CHILD | WS_VISIBLE | ES_BASELINE, 
							IDC_EDIT, 
							10, 40, size.cx + 4, size.cy + 4, hwnd, 0);
			if(htimeedit_01==0)
				htimeedit_01=timeedit;
	
			SetWindowFont (timeedit, timefont);

			InvalidateRect (timeedit, NULL, TRUE);
			old_edit_proc = SetWindowCallbackProc (timeedit, TimeEditBox);
			SetFocusChild (timeedit);
			#endif
		#if 0
			spin = CreateWindow (CTRL_SPINBOX, 
							"", 
							WS_CHILD | WS_VISIBLE, 
							IDC_SPINBOX, 
							40 + size.cx + 6, 220 + (size.cy - 14) / 2, 20, 20, hWnd, 0);
			SendMessage (spin, SPM_SETTARGET, 0, timeedit);
			#endif
			break;
		}

#if 0
		
    case MSG_PAINT:
        /* Output hello, world */
        hdc = BeginPaint (hwnd);
        TextOut (hdc, 0, 0, "Hello, world! - from my control.");
        EndPaint (hwnd, hdc);
        return 0;
		break;
		#endif
    }

    return DefaultControlProc (hwnd, message, wParam, lParam);
}

static BOOL RegisterMyControl (void)
{
    WNDCLASS MyClass;

    MyClass.spClassName = MY_CTRL_NAME;
    MyClass.dwStyle     = WS_NONE;
    MyClass.dwExStyle   = WS_EX_NONE;
    MyClass.hCursor     = GetSystemCursor (IDC_ARROW);
    MyClass.iBkColor    = COLOR_lightwhite;
    MyClass.WinProc     = MyControlProc;

    return RegisterWindowClass (&MyClass);
}

static void UnregisterMyControl (void)
{
    UnregisterWindowClass (MY_CTRL_NAME);
}


static int HelloWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
    switch (message) {
        case MSG_CREATE:
            mycontrol_01=CreateWindow (MY_CTRL_NAME, "", WS_VISIBLE, IDC_STATIC, 0, 0, 200, 60, hWnd, 0);
			
            //mycontrol_01=
				CreateWindow (MY_CTRL_NAME, "", WS_VISIBLE, IDC_STATIC, 0, 60, 200, 60, hWnd, 0);
            return 0;
			

        case MSG_DESTROY:
            DestroyAllControls (hWnd);
            return 0;

        case MSG_CLOSE:
            DestroyMainWindow (hWnd);
            PostQuitMessage (hWnd);
            return 0;
    }

    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}

int MiniGUIMain (int argc, const char* argv[])
{
    MSG Msg;
    HWND hMainWnd;
    MAINWINCREATE CreateInfo;

#ifdef _MGRM_PROCESSES
    JoinLayer(NAME_DEF_LAYER , "mycontrol" , 0 , 0);
#endif

    RegisterMyControl ();

    CreateInfo.dwStyle = WS_VISIBLE | WS_BORDER | WS_CAPTION;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "Hello, world";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = HelloWinProc;
    CreateInfo.lx = 0;
    CreateInfo.ty = 0;
    CreateInfo.rx = 240;
    CreateInfo.by = 280;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = 0;
    CreateInfo.hHosting = HWND_DESKTOP;
    
    hMainWnd = CreateMainWindow (&CreateInfo);
    
    if (hMainWnd == HWND_INVALID)
        return -1;

    ShowWindow(hMainWnd, SW_SHOWNORMAL);
	//SetFocusChild (mycontrol_01);
    while (GetMessage(&Msg, hMainWnd)) {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }

    MainWindowThreadCleanup (hMainWnd);
    UnregisterMyControl ();

    return 0;
}

#ifdef _MGRM_THREADS
#include <minigui/dti.c>
#endif

